<?php
/* * 
 * 功能：支付宝页面跳转同步通知页面
 */
require '../../../source/class/class_core.php';
$discuz = C::app();
$discuz->init();

$settings=C::t('#aljbd#aljbd_setting')->range();
if(!isset($_G['cache']['plugin'])) {
	loadcache('plugin');
}
$is_openarray = array('iswatermark','is_daohang','alipay','malipay','isextcredit','pic','isgo','isnews','isyouh','ispd','isrewrite','islogo','isqq','ista','sjurl','sj_index_lz','time');
foreach($settings as $k => $v){
	if(in_array($k,$is_openarray)){//开关判断
		if($v['value'] == 1){
			$_G['cache']['plugin']['aljbd'][$k] = 1;
		}elseif($v['value'] == 2){
			$_G['cache']['plugin']['aljbd'][$k] = 0;
		}
	}else{
		if($v['value']){
			$_G['cache']['plugin']['aljbd'][$k] = htmlspecialchars_decode($v['value']);//同名变量，有值覆盖设置中变量
		}
	}
}
$alipay_config['partner'] = trim($settings['ec_partner']['value']);

//安全检验码，以数字和字母组成的32位字符
//如果签名方式设置为“MD5”时，请设置该参数
$alipay_config['key']			= $settings['ec_securitycode']['value'];

//商户的私钥（后缀是.pen）文件相对路径
//如果签名方式设置为“0001”时，请设置该参数
$alipay_config['private_key_path']	= 'source/plugin/aljbd/key/rsa_private_key.pem';

//支付宝公钥（后缀是.pen）文件相对路径
//如果签名方式设置为“0001”时，请设置该参数
$alipay_config['ali_public_key_path']= 'source/plugin/aljbd/key/alipay_public_key.pem';


//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑


//签名方式 不需修改
$alipay_config['sign_type']    = 'MD5';

//字符编码格式 目前支持 gbk 或 utf-8
$alipay_config['input_charset']= 'utf8';

//ca证书路径地址，用于curl中ssl校验
//请保证cacert.pem文件在当前文件夹目录中
$alipay_config['cacert']    = getcwd().'/source/plugin/aljbd'.'\\cacert.pem';

//访问模式,根据自己的服务器是否支持ssl访问，若支持请选择https；若不支持请选择http
$alipay_config['transport']    = 'http';

require "lib/alipay_notify.class.php";

//计算得出通知验证结果
$alipayNotify = new AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyReturn();
$_G['siteurl'] = 'http://'.$_SERVER['HTTP_HOST'];
if($verify_result) {//验证成功
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//请在这里加上商户的业务逻辑程序代码
	
	//——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
    //获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表

	//商户订单号
	$out_trade_no = $_GET['out_trade_no'];

	//支付宝交易号
	$trade_no = $_GET['trade_no'];

	//交易状态
	$result = $_GET['result'];


	$order = C::t('#aljbd#aljbd_order') -> fetch($out_trade_no);
	$good = C::t('#aljbd#aljbd_goods') -> fetch($order['sid']);
	if ($order['status'] == 1){
		$status = 2;
		C::t('#aljbd#aljbd_order')->update($out_trade_no, array('status' => $status, 'buyer' => "$trade_no", 'confirmdate' => $_G['timestamp']));
		C::t('#aljbd#aljbd_goods')->update_num2_by_id($good['id'],$order['amount']);
		
		notification_add($order['uid'], 'system',str_replace('{username}',$order['username'],str_replace('{shopname}','<a href="plugin.php?id=aljbd&act=goodview&gid='.$good['id'].'">'.$good['name'].'</a>',$_G['cache']['plugin']['aljbd']['suctips'])));
		$brand = C::t('#aljbd#aljbd') -> fetch($good['bid']);
		notification_add($good['uid'], 'system',str_replace('{orderid}','<a href="plugin.php?id=aljbd&act=orderlist">'.$out_trade_no.'</a>',str_replace('{username}',$brand['username'],str_replace('{name}',$order['username'],str_replace('{shopname}','<a href="plugin.php?id=aljbd&act=goodview&gid='.$good['id'].'">'.$good['name'].'</a>',$_G['cache']['plugin']['aljbd']['notify_merchant'])))));
		if($_G['cache']['plugin']['aljbd']['time']){
			$email_first = C::t("common_member")->fetch($order['uid']);
			$email = $email_first['email'];
			if($email_first['email']){
				$m=str_replace('{username}',$order['username'],str_replace('{shopname}','<a href="'.$_G['siteurl'].'/plugin.php?id=aljbd&act=goodview&gid='.$good['id'].'">'.$good['name'].'</a>',$_G['cache']['plugin']['aljbd']['suctips']));
				require_once libfile('function/mail');
				sendmail_cron($email,$_G['cache']['plugin']['aljbd']['mailtitle'].'@'.gmdate('Y-m-d H:i:s',$_G['timestamp']+8*3600),$m);
			}
			$email_f = C::t("common_member")->fetch($good['uid']);
			$e = $email_f['email'];
			if($email_f['email']){
				$me=str_replace('{orderid}','<a href="'.$_G['siteurl'].'/plugin.php?id=aljbd&act=orderlist">'.$out_trade_no.'</a>',str_replace('{username}',$brand['username'],str_replace('{name}',$order['username'],str_replace('{shopname}','<a href="'.$_G['siteurl'].'/plugin.php?id=aljbd&act=goodview&gid='.$good['id'].'">'.$good['name'].'</a>',$_G['cache']['plugin']['aljbd']['notify_merchant']))));
				$t=str_replace('{orderid}',$out_trade_no,$_G['cache']['plugin']['aljbd']['notify_merchant_title']);
				require_once libfile('function/mail');
				sendmail_cron($e,$t,$me);
			}
		}
	}
	
	dheader('location: '.$_G['siteurl'].'/plugin.php?id=aljbd&act=orderlist');
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
else {
    //验证失败
    //如要调试，请看alipay_notify.php页面的verifyReturn函数
   
}
?>
